package cn.leetcode;

import java.util.ArrayList;
import java.util.List;

public class Code39 {
    public static void main(String[] args) {
        Code39Demo code39Demo = new Code39Demo();
        List<List<Integer>> lists = code39Demo.combinationSum(new int[]{2,3,6,7}, 7);
        System.out.println(lists.toString());
    }
}



class Code39Demo {
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        List<List<Integer>> result = new ArrayList<>();
        ArrayList<Integer> container = new ArrayList<>();
        fun(candidates, target, result, container, 0);
        return result;
    }

    private void fun(int[] candidates, int target, List<List<Integer>> result, List<Integer> list, int j){
        int num = 0;
        for(Integer in : list){
            num = num + in;
        }
        if(num == target){
            result.add(new ArrayList<>(list));
        } else if(num > target)
            return;
        for (int i=j; i< candidates.length; i++){
            list.add(candidates[i]);
            fun(candidates, target, result, list, j);
            list.remove(list.size()-1);
            j++;
        }
    }
}
